/**
 * fileName: SL_YLES_Import_item_barcode_History
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(["require", "exports", "N/search", "N/ui/serverWidget", "N/runtime", "N/url", "../tools/YLES_UI_FORM", "../tools/YLES_RELATED_RECORDTYPE"], function (require, exports, search, ui, runtime, url, YLES_UI_FORM_1, YLES_RELATED_RECORDTYPE_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.onRequest = void 0;
    let req, res, params;
    function onRequest(cxt) {
        req = cxt.request;
        res = cxt.response;
        params = req.parameters;
        if (req.method === 'GET') {
            if (params.logId > 0) {
                let irForm = ui.createForm({ title: 'Import Result Detail' });
                irForm.clientScriptModulePath = './CS_YLES_Import_item_barcode_History.js';
                //创建导入详情界面
                (0, YLES_UI_FORM_1.createImportResultForm)(params.logId, irForm);
                res.writePage(irForm);
            }
            else {
                let ihForm = createImportHistoryForm();
                res.writePage(ihForm);
            }
        }
    }
    exports.onRequest = onRequest;
    //创建所有导入明细界面
    function createImportHistoryForm() {
        const ihForm = ui.createForm({ title: 'Import History' });
        const sublist = ihForm.addSublist({ id: 'info_list', label: 'Details', type: ui.SublistType.STATICLIST });
        sublist.addField({ id: 'line_file', label: 'File Name', type: ui.FieldType.TEXT });
        sublist.addField({ id: 'line_state', label: 'Status', type: ui.FieldType.TEXT });
        sublist.addField({ id: 'line_created', label: 'Created Time', type: ui.FieldType.TEXT });
        sublist.addField({ id: 'line_completed_time', label: 'Completed Time', type: ui.FieldType.TEXT });
        sublist.addField({ id: 'line_link', label: 'Detail', type: ui.FieldType.TEXT });
        const importHistory = getImportLogList();
        importHistory.map(function (ih, index) {
            const link = url.resolveScript({
                scriptId: 'customscript_yles_import_csv_history_sl',
                deploymentId: 'customdeploy_yles_import_csv_history_sl', params: { logId: ih.id }
            });
            const html = `<a href="${link}" target="_blank" class="dottedlink">View Detail</a>`;
            sublist.setSublistValue({ id: 'line_file', value: ih.fileName, line: index });
            sublist.setSublistValue({ id: 'line_state', value: ih.state, line: index });
            sublist.setSublistValue({ id: 'line_created', value: ih.created, line: index });
            if (ih.completedTime) {
                sublist.setSublistValue({ id: 'line_completed_time', value: ih.completedTime, line: index });
            }
            sublist.setSublistValue({ id: 'line_link', value: html, line: index });
        });
        return ihForm;
    }
    //获取所有导入明细
    function getImportLogList() {
        let cur_user = runtime.getCurrentUser().id;
        let Search = search.create({
            type: (0, YLES_RELATED_RECORDTYPE_1.CustomRecordType)().IMPORT_ITEM_BARCODE_LOG,
            filters: [
                ['custrecord_yles_status', 'anyof', [3, 6]], 'and',
                ['owner.internalid', 'anyof', cur_user]
            ],
            columns: [
                { name: 'custrecord_yles_csv_name' },
                { name: 'custrecord_yles_status' },
                { name: 'created', sort: search.Sort.DESC },
                { name: 'custrecord_yles_end_time' },
            ]
        });
        let logList = [];
        Search.run().each(function (res) {
            logList.push({
                id: res.id,
                fileName: res.getValue(Search.columns[0]),
                state: res.getText(Search.columns[1]),
                created: res.getValue(Search.columns[2]),
                completedTime: res.getValue(Search.columns[3])
            });
            return true;
        });
        return logList;
    }
});
